home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume91 / news / barn_201 / part01 / reply.c < prev    next >
C/C++ Source or Header  |  1991-02-07  |  9KB  |  308 lines

  1. /*
  2.  *    File Name:        reply.c
  3.  *    Project:        BARN - Bah's Amiga ReadNews.
  4.  *    Purpose:        Reply via email and post followup article functions.
  5.  *    Functions:        Reply, Followup, IncludeArticle, Sign.
  6.  *    Author:            Jeff Van Epps
  7.  *    Created:        21 Oct 90
  8.  *    Last Modified:    05 Jan 91
  9.  *    Comments:
  10.  *    History:
  11.  *        21 Oct 90/JVE    Created.
  12.  *        14 Nov 90/JVE    Post news by mailing article to inews@bisco rather
  13.  *                        than <newsgroup>@ucbvax.berkeley.edu.  Only mail the
  14.  *                        article if the user saved his edit.  Same for
  15.  *                        email replies.
  16.  *        05 Jan 91/JVE    Use article->subject optimization.
  17.  */
  18.  
  19. # include    <stdio.h>
  20. # include    <stdlib.h>
  21. # include    <string.h>
  22. # include    <time.h>
  23. # include    <sys/types.h>
  24. # include    <sys/stat.h>
  25. # include    "standard.h"
  26. # include    "configure.h"
  27. # include    "variables.h"
  28. # include    "article.h"
  29. # include    "reply.h"
  30.  
  31. # ifdef sun
  32.  
  33. # define    DEFAULT_EDITOR    "vi"
  34. # define    TEMPFILE        "/tmp/arnreply"
  35.  
  36. # else  /* not sun */
  37.  
  38. # define    DEFAULT_EDITOR    "stevie"
  39. # define    TEMPFILE        "T:arnreply"
  40.  
  41. # endif /* sun */
  42.  
  43.  
  44. /****************************************************************************/
  45. /*    FUNCTION:    Reply                                                        */
  46. /*                                                                            */
  47. /*    PURPOSE:    Send a reply to a news article via mail.                    */
  48. /*                                                                            */
  49. /*    INPUT PARAMETERS:                                                        */
  50. /*        NAME        I/O        DESCRIPTION                                        */
  51. /*        ----        ---        -----------                                        */
  52. /*        article         I        Article to reply to.                            */
  53. /*        include         I        Include article in reply?                        */
  54. /*                                                                            */
  55. /*    RETURNS: none                                                            */
  56. /*                                                                            */
  57. /*    COMMENTS:                                                                */
  58. /*                                                                            */
  59. /*    HISTORY:                                                                */
  60. /*        1.    17 Oct 90        Created.                                        */
  61. /*        2.    14 Nov 90        Do not invoke mailer if user has not edited.    */
  62. /*                                                                            */
  63. /****************************************************************************/
  64.  
  65. void Reply( article, include )
  66.  
  67. ARTICLE_INFO        *article;
  68. int                    include;
  69.  
  70. {
  71. FILE                *fp;
  72. char                *from, *address, *editor, *hdr;
  73. char                cmd[MAXLINE], temp[MAXLINE];
  74. long                clock;                            /* current date & time */
  75. int                    header_found = FALSE;            /* parsed To: yet? */
  76. struct stat            stats_before, stats_after;
  77.  
  78. remove( TEMPFILE );
  79. if ( ( fp = fopen( TEMPFILE, "a" ) ) == NULL )
  80.     {
  81.     perror( TEMPFILE );
  82.     return;
  83.     }
  84. time( &clock );
  85. fprintf( fp, "From %s %24.24s remote from %s\n", GetVar( VAR_USER), 
  86.   ctime( &clock ), GetVar( VAR_NODE ) );
  87. fprintf( fp, "Date: %s", ctime( &clock ) );
  88. fprintf( fp, "From: %s!%s@%s (%s)\n", GetVar( VAR_NODE ), GetVar( VAR_USER ), 
  89.   GetVar( VAR_DOMAIN ), GetVar( VAR_NAME ) );
  90. /*
  91.  *    Use everything after From: up to the first space.
  92.  */
  93. from = strtok( GetHeader( article -> headers, HDR_FROM ), " \n\r" );
  94. fprintf( fp, "%s: %s\n", HDR_TO, from );
  95. fprintf( fp, "%s: \n", HDR_CC );
  96. fprintf( fp, "%s: %s\n\n", HDR_SUBJECT, article -> subject );
  97. if ( include )
  98.     IncludeArticle( article, fp );
  99. Sign( fp );
  100. fclose( fp );
  101. (void) stat( TEMPFILE, &stats_before );
  102. if ( ( editor = GetVar( VAR_EDITOR ) ) == NULL )
  103.     editor = DEFAULT_EDITOR;
  104. sprintf( cmd, "%s %s", editor, TEMPFILE );
  105. system( cmd );
  106. (void) stat( TEMPFILE, &stats_after );
  107. if ( stats_after.st_mtime != stats_before.st_mtime )
  108.     {
  109.     if ( ( fp = fopen( TEMPFILE, "r" ) ) == NULL )
  110.         {
  111.         perror( TEMPFILE );
  112.         return;
  113.         }
  114.     while ( ! header_found && fgets( temp, MAXLINE, fp ) != NULL )
  115.         {
  116.         if ( ( hdr = strtok( temp, ": " ) ) != NULL && strcmp( hdr, HDR_TO ) == 0 )
  117.             header_found = TRUE;
  118.         }
  119.     if ( ! header_found )
  120.         {
  121.         printf( "Header messed!\n" );
  122.         fclose( fp );
  123.         return;
  124.         }
  125.     if ( ( address = strtok( NULL, " " ) ) == NULL )
  126.         {
  127.         printf( "No address given!\n" );
  128.         fclose( fp );
  129.         return;
  130.         }
  131.     fclose( fp );
  132.     sprintf( cmd, "rmail <%s >NULL: %s", TEMPFILE, address );
  133.     system( cmd );
  134.     }
  135. else
  136.     {
  137.     printf( "File not modified.  Mail not sent.  Hit any key.\n" );
  138.     getchar();
  139.     }
  140. remove( TEMPFILE );
  141. }
  142.  
  143. /****************************************************************************/
  144. /*    FUNCTION:    Followup                                                    */
  145. /*                                                                            */
  146. /*    PURPOSE:    Post news article as a reply to a news article.                */
  147. /*                                                                            */
  148. /*    INPUT PARAMETERS:                                                        */
  149. /*        NAME        I/O        DESCRIPTION                                        */
  150. /*        ----        ---        -----------                                        */
  151. /*        newsgroup     I        Name of newsgroup.                                */
  152. /*        article         I        Article to followup.                            */
  153. /*        include         I        Include article in reply?                        */
  154. /*                                                                            */
  155. /*    RETURNS: none                                                            */
  156. /*                                                                            */
  157. /*    COMMENTS:                                                                */
  158. /*        Changed to send mail to inews@bisco.  Newsgroup name is embedded    */
  159. /*        in the article.                                                        */
  160. /*                                                                            */
  161. /*        The newsgroup name has all dots or slashes turned into dashes, then    */
  162. /*        mail is sent to <modified newsgroup name>@ucbvax.berkeley.edu,        */
  163. /*        which is a gateway for posting.                                        */
  164. /*                                                                            */
  165. /*    HISTORY:                                                                */
  166. /*        1.    17 Oct 90        Created.                                        */
  167. /*        2.    14 Nov 90        Mail to bisco rather than Berkeley.  Only        */
  168. /*                            mail the article if it has been edited by user.    */
  169. /*                                                                            */
  170. /****************************************************************************/
  171.  
  172. void Followup( newsgroup, article, include )
  173.  
  174. char                *newsgroup;
  175. ARTICLE_INFO        *article;
  176. int                    include;
  177.  
  178. {
  179. FILE                *fp;
  180. char                *editor;
  181. char                cmd[MAXLINE];
  182. long                clock;                            /* current date & time */
  183. struct stat            stats_before, stats_after;
  184.  
  185. remove( TEMPFILE );
  186. if ( ( fp = fopen( TEMPFILE, "a" ) ) == NULL )
  187.     {
  188.     perror( TEMPFILE );
  189.     return;
  190.     }
  191. time( &clock );
  192. fprintf( fp, "From %s %24.24s remote from %s\n", GetVar( VAR_USER ), 
  193.   ctime( &clock ), GetVar( VAR_NODE ) );
  194. fprintf( fp, "Date: %s", ctime( &clock ) );
  195. fprintf( fp, "From: %s!%s@%s (%s)\n", GetVar( VAR_NODE ), GetVar( VAR_USER ), 
  196.   GetVar( VAR_DOMAIN ), GetVar( VAR_NAME ) );
  197. fprintf( fp, "%s: %s\n", HDR_SUBJECT, article -> subject );
  198. fprintf( fp, "Newsgroups: %s\n", newsgroup );
  199. fprintf( fp, "Followup-To: %s\n", newsgroup );
  200. fprintf( fp, "Distribution: world\n" );
  201. fprintf( fp, "Reply-To: %s!%s@%s\n", GetVar( VAR_NODE ), GetVar( VAR_USER ),
  202.   GetVar( VAR_DOMAIN ) );
  203. fprintf( fp, "\n" );        /* separate headers from body of article */
  204. if ( include )
  205.     IncludeArticle( article, fp );
  206. Sign( fp );
  207. fclose( fp );
  208. (void) stat( TEMPFILE, &stats_before );
  209. if ( ( editor = GetVar( VAR_EDITOR ) ) == NULL )
  210.     editor = DEFAULT_EDITOR;
  211. sprintf( cmd, "%s %s", editor, TEMPFILE );
  212. system( cmd );
  213. (void) stat( TEMPFILE, &stats_after );
  214. if ( stats_after.st_mtime != stats_before.st_mtime )
  215.     {
  216.     sprintf( cmd, "rmail <%s >NULL: inews@bisco", TEMPFILE );
  217.     system( cmd );
  218.     }
  219. else
  220.     {
  221.     printf( "File not modified.  Article not posted.  Hit any key.\n" );
  222.     getchar();
  223.     }
  224. remove( TEMPFILE );
  225. }
  226.  
  227.  
  228. /****************************************************************************/
  229. /*    FUNCTION:    IncludeArticle                                                */
  230. /*                                                                            */
  231. /*    PURPOSE:    Copy article text to file as an inclusion.                    */
  232. /*                                                                            */
  233. /*    INPUT PARAMETERS:                                                        */
  234. /*        NAME        I/O        DESCRIPTION                                        */
  235. /*        ----        ---        -----------                                        */
  236. /*        article         I        Article being included.                            */
  237. /*        output         I        File being written into.                        */
  238. /*                                                                            */
  239. /*    RETURNS: none                                                            */
  240. /*                                                                            */
  241. /*    COMMENTS:                                                                */
  242. /*                                                                            */
  243. /*    HISTORY:                                                                */
  244. /*        1.    21 Oct 90         Created.                                        */
  245. /*                                                                            */
  246. /****************************************************************************/
  247.  
  248. void IncludeArticle( article, output )
  249.  
  250. ARTICLE_INFO            *article;
  251. FILE                    *output;
  252.  
  253. {
  254. char                    filename[MAXLINE];
  255. char                    s[MAXLINE];
  256. FILE                    *art;
  257.  
  258. sprintf( filename, "%ld", article -> number );
  259. if ( ( art = fopen( filename, "r" ) ) == NULL )
  260.     printf( "CAN'T OPEN ARTICLE FOR INCLUDE!\n" );
  261. else
  262.     {
  263.     fseek( art, article -> textpos, 0 );
  264.     while ( fgets( s, MAXLINE, art ) != NULL )
  265.         fprintf( output, "> %s", s );
  266.     fclose( art );
  267.     }
  268. }
  269.  
  270.  
  271. /****************************************************************************/
  272. /*    FUNCTION:    Sign                                                        */
  273. /*                                                                            */
  274. /*    PURPOSE:    Append users .signature.                                    */
  275. /*                                                                            */
  276. /*    INPUT PARAMETERS:                                                        */
  277. /*        NAME        I/O        DESCRIPTION                                        */
  278. /*        ----        ---        -----------                                        */
  279. /*        output         I        File being written to.                            */
  280. /*                                                                            */
  281. /*    RETURNS: none                                                            */
  282. /*                                                                            */
  283. /*    COMMENTS:                                                                */
  284. /*        There is no default .signature file.                                */
  285. /*                                                                            */
  286. /*    HISTORY:                                                                */
  287. /*        1.    21 Oct 90        Created.                                        */
  288. /*                                                                            */
  289. /****************************************************************************/
  290.  
  291. void Sign( output )
  292.  
  293. FILE                *output;
  294.  
  295. {
  296. FILE                *sig;
  297. char                *sig_file;        /* filename for .signature */
  298. char                s[MAXLINE];
  299.  
  300. if ( ( sig_file = GetVar( VAR_SIGN ) ) != NULL && 
  301.   ( sig = fopen( sig_file, "r" ) ) != NULL )
  302.     {
  303.     while ( fgets( s, MAXLINE, sig ) != NULL )
  304.         fprintf( output, "%s", s );
  305.     fclose( sig );
  306.     }
  307. }
  308.